<?php

namespace app\home\controller;

use app\common\controller\Home;

class Other extends Home
{
	public function Index()
	{
		return 'index';
	}

	public function getUrl()
	{
		$Url = zanpiancms_param_url();
		$id = $Url['name'];
		$type = $Url['type'];
		$playurl = $id;
		if ($type === '360') {
			$url = 'http://j.99496.com/play/' . $id;
			$playurl = $this->reUrl($url);
		} elseif ($type === 'qqq') {
			$url = 'http://quan.qq.com/video/1098_' . $id;
			$data = explode('/', $this->get_redirect_url($url));
			$playurl = 'https://apd-videohy.apdcdn.tc.qq.com/' . $data[3] . '/' . $data[4];
		} elseif ($type === 'iqiyi') {
			$data = file_get_contents("https://pcw-api.iqiyi.com/video/video/baseinfo/" . $id);
			preg_match('#\"playUrl\":"(.*?)\"#', $data, $tvid);
			$playurl = $tvid[1];
		}
		return json(['data' => $playurl, 'info' => 'ok', 'status' => 1]);
	}

	private function object_array($array)
	{
		if (is_object($array)) {
			$array = (array) $array;
		}
		if (is_array($array)) {
			foreach ($array as $key => $value) {
				$array[$key] = $this->object_array($value);
			}
		}
		return $array;
	}

	private function reUrl($url, $json = '')
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		$info = !empty($json) ? $this->object_array(json_decode(curl_exec($ch))) : curl_exec($ch);
		curl_close($ch);
		return !empty($json) ? $info['url'] : $info;
	}

	private function get_redirect_url($url, $referer = '', $timeout = 10)
	{
		$redirect_url = false;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, TRUE);
		curl_setopt($ch, CURLOPT_NOBODY, TRUE); //不返回请求体内容
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); //允许请求的链接跳转
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'Connection: Keep-Alive'));
		if ($referer) {
			curl_setopt($ch, CURLOPT_REFERER, $referer); //设置referer
		}
		$content = curl_exec($ch);
		if (!curl_errno($ch)) {
			$redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //获取最终请求的url地址
		}
		return $redirect_url;
	}
}
